/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modules.estoque.models.dao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import modules.estoque.util.Funcoes;
import modules.estoque.util.HibernateUtil;
import org.hibernate.SessionFactory;

/**
 *
 * @author Clezio
 */
public class AbstractDAO {
    private SessionFactory sessionFactory;

     public AbstractDAO() throws Exception{
         sessionFactory = HibernateUtil.getSessionFactory();
     }

    /**
     * @return the sessionFactory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public static Connection obterConexao() throws Exception{
        Connection conn = null;
        try {
            Class.forName(HibernateUtil.getCfg().getProperty("hibernate.connection.driver_class"));
            conn = DriverManager.getConnection(
                    HibernateUtil.getCfg().getProperty("hibernate.connection.url"),
                    HibernateUtil.getCfg().getProperty("hibernate.connection.username"),
                    HibernateUtil.getCfg().getProperty("hibernate.connection.password")
                    );
        } catch (Exception ex) {
            Funcoes.registrarLogErro(ex);
        }
        return conn;
    }
    
    protected Object montarDtoRetorno(Object object) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
        Class clazz = Class.forName(object.getClass().getName());
        clazz.isInstance(object);
        Class clazzRetorno = Class.forName(object.getClass().getName());
        Object objectRetorno = clazzRetorno.newInstance();
        Method[] metodos = clazz.getDeclaredMethods();
        for(int x=0;x<metodos.length;x++){
            int startMetodo = 0;
            if(metodos[x].getName().startsWith("is")){
                startMetodo = 2;
            }else if(metodos[x].getName().startsWith("get")){
                startMetodo = 3;
            }
            
            if(startMetodo > 0){
                //obter
                Method method = clazz.getMethod(metodos[x].getName());
                Object retobj = method.invoke(object); 
                //setar
                String metodoSetar = "set"+metodos[x].getName().substring(startMetodo, metodos[x].getName().length());
                Class partypes[] = new Class[1]; 
                partypes[0] = method.getReturnType();
                if(!metodoSetar.equals("setHibernateLazyInitializer")){
                    Method method2 = clazz.getMethod(metodoSetar,partypes);
                    Object arglist[] = new Object[1];  
                    arglist[0] = retobj;  
                    method2.invoke(objectRetorno,arglist); 
                }
            }
        }
        
        return objectRetorno;
    }
}
